<?php
/**
 * @file $HeadURL: attendanceworkday.php $
* @author $Author: ZhangFan (zhangfan1@haibao.com) $
* @date $Date: 2016-1-13 $
* @brief
*/

namespace  haibao\cms\business\attendance;

use haibao\cms\data\attendance\AttendanceWorkDay as AttendanceData;
use haibao\app\business\BusinessException;
use haibao\frame\data\query\Condition;
class AttendanceWorkDay extends \haibao\cms\business\Base{

    public static function addWorkDay($model){
        self::checkOperation(self::FUNC_CODE_ATTENDANCE_WORK_DAY_ADD);
        if(empty($model->WorkDate)){
            throw new BusinessException('请添加时间');
        }
        $attendanceData = new AttendanceData();
        $attendanceData->add($model);
        self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_WORK_DAY_ADD, $model);
    }
    public static function isExitWorkDate($workDate){
        $attendanceData = new AttendanceData();
        $result = $attendanceData->isExitWorkDate($workDate);
        if($result){
            return true;
        }else{
            return false;
        }
    }
    public static function updateWorkDay($workDate,$status){
        $attendanceData = new AttendanceData();
        $attendanceData->updateWorkDay($workDate,$status);
    }
    public static function getWorkDate($filter){
    	$attendanceData = new AttendanceData();
    	return $attendanceData->getAll($filter);
    }
    public static function deleteWorkDayById($workDayId){
        self::checkOperation(self::FUNC_CODE_ATTENDANCE_WORK_DAY_DEL);
        if(!$workDayId){
             throw new BusinessException('Id不能为空');
        }
        $attendanceData = new AttendanceData();
        $attendanceData->removeById($workDayId);
        self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_WORK_DAY_DEL, $workDayId);
    }
    
    public static function getWorkDayByDate($date){
    	if(!$date){
    		throw new BusinessException('日期不能为空',self::DATA_EMPTY);
    	}
    	$filter = new \haibao\cms\model\filter\attendance\AttendanceWorkDay();
    	$filter->where(\haibao\cms\model\filter\attendance\AttendanceWorkDay::CONDITION_FIELD_NAME_WOEKDATE,Condition::CONDITION_EQUAL,$date);
    	
        $data = new AttendanceData();
        
        return $data->getOne($filter);
    }
    
    public static function getWorkDateByDataRange($startDate,$endDate){
    	$filter = new \haibao\cms\model\filter\attendance\AttendanceWorkDay();
    	$filter->where(\haibao\cms\model\filter\attendance\AttendanceWorkDay::CONDITION_FIELD_NAME_WOEKDATE,Condition::CONDITION_GREATEREQUAL,$startDate);
    	$filter->where(\haibao\cms\model\filter\attendance\AttendanceWorkDay::CONDITION_FIELD_NAME_WOEKDATE,Condition::CONDITION_LESSEQUAL,$endDate);
    	$filter->where(\haibao\cms\model\filter\attendance\AttendanceWorkDay::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,\haibao\cms\model\data\attendance\AttendanceWorkDay::STATUS_WORK);
    	$data = new AttendanceData();
    	
    	$workDateArr = array();
    	$workDateList = $data->getAll($filter);
    	foreach($workDateList as $list){
    		array_push($workDateArr,$list->WorkDate->format('Y-m-d'));
    	}
    	return $workDateArr;
    }
}